package com.ttwm.controller;

import com.ttwm.pojo.Comment;
import com.ttwm.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class CommentController {
    @Autowired
    private CommentService commentService;

    @PostMapping(value = {"/ttwm/addComment"})
    public Map<String ,Object> addComment(@RequestBody Comment comment){
        Map map = new HashMap<String ,Object>();
        int add = commentService.addComment(comment);
        if (add>0){
            map.put("success",true);
            map.put("msg","成功");
        }else {
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }

    @PostMapping(value = {"/ttwm/commentList"})
    public Map<String ,Object> commentList(@RequestBody Comment comment){
        Map map = new HashMap<String ,Object>();
        List<Comment> commentList = commentService.commentList(comment);
        if (commentList.isEmpty()){
            map.put("success",false);
            map.put("msg","失败");
        }else {
            map.put("success",true);
            map.put("msg","成功");
            map.put("list",commentList);
        }
        return map;
    }

    @PostMapping(value = {"/ttwm/oneComment"})
    public Map<String ,Object> oneComment(@RequestBody Comment comment){
        Map map = new HashMap<String ,Object>();
        Comment oneComment = commentService.oneComment(comment);
        if (oneComment != null){
            map.put("success",true);
            map.put("msg","成功");
            map.put("data",oneComment);
        }else {
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }

    @PostMapping(value = {"/ttwm/deleteComment"})
    public Map<String ,Object> deleteComment(@RequestBody Comment comment){
        Map map = new HashMap<String ,Object>();
        int deleteComment = commentService.deleteComment(comment);
        if (deleteComment>0){
            map.put("success",true);
            map.put("msg","成功");
        }else {
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }

    @PostMapping(value = {"/ttwm/updateComment"})
    public Map<String ,Object> updateComment(@RequestBody Comment comment){
        Map map = new HashMap<String ,Object>();
        int updateComment = commentService.updateComment(comment);
        if (updateComment>0){
            map.put("success",true);
            map.put("msg","成功");
        }else {
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }

    @PostMapping(value = {"/ttwm/deleteCommentAll"})
    public Map<String ,Object> deleteCommentAll(@RequestBody Comment comment){
        Map map = new HashMap<String ,Object>();
        int deleteComment = commentService.deleteCommentAll(comment);
        if (deleteComment>0){
            map.put("success",true);
            map.put("msg","成功");
        }else {
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }

}
